<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Web高级开发 | Advanced Web Development Course</title>
    <link rel="stylesheet" href="../../css/main.css" />
    <link rel="stylesheet" href="../../css/tabs.css" />
    <script src="../../js/uikit.js"></script>
    <link href="../../css/prism.css" rel="stylesheet" />
    <script src="../../js/prism.js"></script>
  </head>

  <body>
    <div
      data-uk-sticky="animation: uk-animation-slide-top; sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; cls-inactive: uk-navbar-transparent; top: 300"
    >
      <nav class="uk-navbar-container pt-2 pb-2">
        <div class="uk-container">
          <div data-uk-navbar>
            <div class="uk-navbar-left">
              <a
                class="uk-navbar-item uk-logo uk-visible@m"
                href="../../index.html"
                >Web高级开发课程</a
              >
              <a
                class="uk-navbar-tuk-containeroggle uk-hidden@m"
                href="#offcanvas-docs"
                data-uk-toggle
                ><span data-uk-navbar-toggle-icon></span>
                <span class="uk-margin-small-left">目录</span></a
              >
              <ul class="uk-navbar-nav uk-visible@m">
                <li><a href="../../index.html">文档</a></li>
              </ul>
            </div>
            <div class="uk-navbar-center uk-hidden@m">
              <a class="uk-navbar-item uk-logo" href="../../index.html"
                >Web高级开发课程</a
              >
            </div>
          </div>
        </div>
      </nav>
    </div>

    <div class="uk-section">
      <div class="uk-container">
        <div class="uk-grid-large" data-uk-grid>
          <div class="sidebar-fixed-width uk-visible@m">
            <div class="sidebar-docs uk-position-fixed uk-margin-top">
              <h5>关于这门课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../../index.html">大纲</a></li>
              </ul>
              <h5>章节</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../01.html">第1章 - Spring基础</a></li>
                <li><a href="../02.html">第2章 - Spring中的Bean</a></li>
                <li><a href="../03.html">第3章 - Spring AOP</a></li>
                <li><a href="../04.html">第4章 - Spring的事物管理</a></li>
                <li><a href="../05.html">第5章 - 初识MyBatis</a></li>
                <li><a href="../06.html">第6章 - MyBatis的核心配置</a></li>
                <li><a href="../07.html">第7章 - MyBatis的高级应用</a></li>
                <li><a href="../08.html">第8章 - Spring MVC入门</a></li>
                <li><a href="../09.html">第9章 - Spring MVC数据绑定</a></li>
                <li><a href="../10.html">第10章 - Spring MVC拦截器</a></li>
                <li><a href="../11.html">第11章 - Spring Boot基础</a></li>
                <li><a href="../12.html">第12章 - Spring Boot的Web开发</a></li>
                <li>
                  <a href="../13.html">第13章 - Spring Boot的数据库连接</a>
                </li>
                <li><a href="../14.html">第14章 - 框架整合</a></li>
              </ul>
              <h5>实验课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="./code-01.html">01</a></li>
                <li><a href="./code-02.html">02</a></li>
                <li><a href="./code-03.html">03</a></li>
                <li><a href="./code-04.html">04</a></li>
                <li><a href="./code-05.html">05</a></li>
                <li><a href="./code-06.html">06</a></li>
                <li><a href="./code-07.html">07</a></li>
                <li class="uk-active"><a href="./code-08.html">08</a></li>
                <li><a href="./code-09.html">09</a></li>
                <li><a href="./code-10.html">10</a></li>
                <li><a href="./code-11.html">11</a></li>
                <li><a href="./code-12.html">12</a></li>
              </ul>
            </div>
          </div>
          <div class="uk-width-1-1 uk-width-expand@m">
            <article class="uk-article">
              <h1>实验课08 - 数据响应 + 拦截器</h1>
<div>
<h3>3.响应数据</h3>
<h4>1.返回字符串</h4>
<p>在controller中加入</p>
<pre><code class='language-java' lang='java'>@RequestMapping(value=&quot;testReturnString&quot;)
public String testReturnString(Model model){
    User user = new User();
    user.setUsername(&quot;abc&quot;);
    user.setPassword(&quot;111&quot;);

    model.addAttribute(&quot;user&quot;, user);

    return &quot;success&quot;;
}
</code></pre>
<p>前端中加入success页面中加入</p>
<pre><code class='language-html' lang='html'>${user.username}
${user.password}
</code></pre>
<p>注意! 同时需要在success.jsp的顶部加上 isELIgnored=&quot;false&quot;， 如下面的代码：</p>
<p>不加 isELIgnored=&quot;false是显示不出数据的。</p>
<pre><code>&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; isELIgnored=&quot;false&quot;%&gt;
</code></pre>
vaiv<p>当请求testReturnString的时候，就会跳转到success.jsp页面，并且显示出username和password，分别是abc和111</p>
<h4>2. 无返回值</h4>
<pre><code class='language-java' lang='java'>@RequestMapping(value=&quot;testReturnVoid&quot;)
public void testReturnVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(&quot;forward...&quot;);
    // 请求转发
    //request.getRequestDispatcher(&quot;/WEB-INF/pages/success.jsp&quot;).forward(request, response);

    // 重定向
    //response.sendRedirect(request.getContextPath()+&quot;/index.jsp&quot;);

    // 直接响应数据
    response.setCharacterEncoding(&quot;UTF-8&quot;);
    response.setContentType(&quot;text/html; charset=UTF-8&quot;);
    response.getWriter().print(&quot;哈哈&quot;);
    return;
}
</code></pre>
<h4>3. ModelAndView</h4>
<p>在controller中添加方法</p>
<pre><code class='language-java' lang='java'>@RequestMapping(value=&quot;getUser&quot;)
public ModelAndView getUser(){
    ModelAndView mv = new ModelAndView();

    // 模拟数据库
    User user = new User();
    user.setUsername(&quot;abc&quot;);
    user.setPassword(&quot;111&quot;);

    mv.addObject(&quot;user&quot;, user);

    mv.setViewName(&quot;success&quot;);

    return mv;
}
</code></pre>
<p>在前端加入</p>
<pre><code class='language-html' lang='html'>&lt;a href=&quot;getUser&quot;&gt;getUser&lt;/a&gt;
</code></pre>
<p>当点击getUser的时候，就会发起getUser的请求，从而调用public ModelAndView getUser() 这个方法。</p>
<h3>4.拦截器</h3>
<h4>1.定义拦截器类</h4>
<pre><code class='language-java' lang='java'>package com.qust.utils;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(&quot;拦截器预处理执行了...&quot;);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println(&quot;拦截器后处理执行了...&quot;);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println(&quot;拦截器完成了操作...&quot;);
    }
}

</code></pre>
<h4>2.配置拦截器</h4>
<p>在springmvc.xml文件中加入</p>
<pre><code class='language-xml' lang='xml'>&lt;mvc:interceptors&gt;
    &lt;mvc:interceptor&gt;
        &lt;!-- 哪些方法进行拦截 --&gt;
        &lt;mvc:mapping path=&quot;/*&quot;/&gt;
        &lt;!-- 哪些方法不进行拦截 --&gt;
        &lt;!-- &lt;mvc:exclude-mapping path=&quot;&quot;/&gt; --&gt;
        &lt;!-- 注册拦截器对象 --&gt;
        &lt;bean class=&quot;com.qust.utils.MyInterceptor&quot;/&gt;
    &lt;/mvc:interceptor&gt;
&lt;/mvc:interceptors&gt;
</code></pre>
<h4>3.拦截器链</h4>
<p>用同样的方式新建一个拦截器类，然后再springmvc.xml里面添加配置</p>
<pre><code class='language-xml' lang='xml'>&lt;mvc:interceptors&gt;
    &lt;mvc:interceptor&gt;
        &lt;!-- 哪些方法进行拦截 --&gt;
        &lt;mvc:mapping path=&quot;/*&quot;/&gt;
        &lt;!-- 哪些方法不进行拦截 --&gt;
        &lt;!-- &lt;mvc:exclude-mapping path=&quot;&quot;/&gt; --&gt;
        &lt;!-- 注册拦截器对象 --&gt;
        &lt;bean class=&quot;com.qust.utils.MyInterceptor&quot;/&gt;
    &lt;/mvc:interceptor&gt;
    &lt;mvc:interceptor&gt;
        &lt;!-- 哪些方法进行拦截 --&gt;
        &lt;mvc:mapping path=&quot;/*&quot;/&gt;
        &lt;!-- 哪些方法不进行拦截 --&gt;
        &lt;!--&lt;mvc:exclude-mapping path=&quot;&quot;/&gt; --&gt;
        &lt;!-- 注册拦截器对象 --&gt;
        &lt;bean class=&quot;com.qust.utils.MyInterceptor2&quot;/&gt;
    &lt;/mvc:interceptor&gt;
&lt;/mvc:interceptors&gt;
</code></pre>
<p>注意这里是要添加一个新的mvc:interceptor标签，而不是简单的在第一个mvc:interceptor标签中添加一个bean</p>

  
</div>
            </article>
          </div>
        </div>
      </div>
    </div>




    <div id="offcanvas-docs" data-uk-offcanvas="overlay: true">
      <div class="uk-offcanvas-bar">
        <button class="uk-offcanvas-close" type="button" data-uk-close></button>
        <h5 class="uk-margin-top">关于这门课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../indext.html">大纲</a></li>
        </ul>
        <h5 class="uk-margin-top">章节</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../01.html">第1章 - Spring基础</a></li>
          <li><a href="../02.html">第2章 - Spring中的Bean</a></li>
          <li><a href="../03.html">第3章 - Spring AOP</a></li>
          <li><a href="../04.html">第4章 - Spring的事物管理</a></li>
          <li><a href="../05.html">第5章 - 初识MyBatis</a></li>
          <li><a href="../06.html">第6章 - MyBatis的核心配置</a></li>
          <li><a href="../07.html">第7章 - MyBatis的高级应用</a></li>
          <li><a href="../08.html">第8章 - Spring MVC入门</a></li>
          <li><a href="../09.html">第9章 - Spring MVC数据绑定</a></li>
          <li><a href="../10.html">第10章 - Spring MVC拦截器</a></li>
          <li><a href="../11.html">第11章 - Spring Boot基础</a></li>
          <li><a href="../12.html">第12章 - Spring Boot的Web开发</a></li>
          <li><a href="../13.html">第13章 - Spring Boot的数据库连接</a></li>
          <li><a href="../14.html">第14章 - 框架整合</a></li>
        </ul>
        <h5>实验课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="./code-01.html">01</a></li>
          <li><a href="./code-02.html">02</a></li>
          <li><a href="./code-03.html">03</a></li>
          <li><a href="./code-04.html">04</a></li>
          <li><a href="./code-05.html">05</a></li>
          <li><a href="./code-06.html">06</a></li>
          <li><a href="./code-07.html">07</a></li>
          <li class="uk-active"><a href="./code-08.html">08</a></li>
          <li><a href="./code-09.html">09</a></li>
          <li><a href="./code-10.html">10</a></li>
          <li><a href="./code-11.html">11</a></li>
          <li><a href="./code-12.html">12</a></li>
        </ul>
      </div>
    </div>

    <footer class="uk-section uk-text-center uk-text-muted">
      <div class="uk-container uk-container-small">
        <div class="uk-margin-medium"></div>
      </div>
    </footer>

    <script src="../js/awesomplete.js"></script>
    <script src="../js/custom.js"></script>
  </body>
</html>
